/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.siit.ke;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import net.siit.ling.Clause;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 *
 * @author arthit
 */
public class ClauseUtil {
    /**
     * 
     * @param list
     * @return
     */
    public static Map<Clause,Long> countClause(List<Clause> list) {
        Map<Clause,Long> clist = new HashMap<Clause,Long>();
        
        for (Clause c : list) {
            if (clist.containsKey(c)) {
                clist.put(c, clist.get(c) + 1L);
            } else {
                clist.put(c, 1L);
            }
        }
        return clist;
    }
    
    /**
     * 
     * @param fileInputStream 
     * @param charset 
     * @return
     * @throws java.io.IOException
     */
    public static List<Clause> collectClause(FileInputStream fileInputStream, String charset) throws IOException {
        ArrayList<Clause> list = new ArrayList<Clause>();

        BufferedReader reader = null;
        reader = new BufferedReader(new InputStreamReader(fileInputStream, charset));
        if (reader == null)
            throw new IOException("Cannot create input reader.");
        
        Pattern p;
        Matcher m;
        try {
            p = Pattern.compile("\\s*\\[(\\S+)\\s+(.+?)\\]\\s*");
        }
        catch(PatternSyntaxException e) {
            return null;
        }
        
        String str;
        long l = 1;
        while ((str = reader.readLine()) != null) {
            str = str.trim();
            //System.err.println("{" + str + "}");
            if (str.length() < 1) {
                l++;
                continue;
            } else
            if (str.charAt(0) != '#') {
                System.err.println("Markup error, line #" + l + ", skip.");
                l++;
                continue;
            } else {
                str = str.substring(1);
            }

            // now can captures clauses only, not connectors
            Clause c = new Clause();

            m = p.matcher(str);
            int endIndex = 0;
                        
            while (m.find()) {
                endIndex = m.end();

                String label = m.group(1);
                String text  = m.group(2);

                //System.err.println(label + " : " + text);
                if (label.equalsIgnoreCase("S")) c.subject = text; else
                    if (label.equalsIgnoreCase("V")) c.verb = text; else
                        if (label.equalsIgnoreCase("O")) c.object = text; else
                            if (label.equalsIgnoreCase("X")) c.context = text;                
            }

            if (endIndex != str.length()) {
                System.err.println("Cannot match entire line, ignore from '" + str.substring(endIndex-1) + "'.");
            }
            
            if (c.subject != null)
                list.add(c);
            l++;
        }
        reader.close();

        return list;
    }

}
